const db = require('../db')

// 获取所有联系人
exports.getAllConnectors = (req, res) => {
  const { wx_id } = req.query
  // console.log(wx_id)

  const selectRelations = 'select * from easy_chat.user_relations where wx_id = ?'
  const selectNickname = 'select nickname, avatar_url, phone from easy_chat.users where wx_id = ?'
  db.query(selectRelations, wx_id, async (err, results) => {
    if (err) return res.errHandle(err)

    // console.log(results)
    const connectors = []
    // 使用 for...of 循环来正确处理异步操作
    for (const connector of results) {
      // 获取 nickname
      try {
        const nicknameRes = await new Promise((resolve, reject) => {
          db.query(selectNickname, connector.friend_id, (err, results) => {
            if (err) reject(err)
            else resolve(results)
          })
        })

        // console.log(nicknameRes)

        nicknameRes.forEach(nick => {
          connectors.push({
            wx_id: connector.friend_id,
            remark: connector.remark,
            relation_status: connector.relation_status,
            nickname: nick.nickname,
            created_at: connector.created_at,
            avatar_url: nick.avatar_url,
            phone: nick.phone,
            introduction: connector.introduction
          })
        })
      } catch (err) {
        return res.errHandle(err)
      }
    }

    res.send({
      code: 200,
      message: 'success',
      data: {
        connectors: connectors
      }
    })
  })
}

// 获取联系人详情
exports.getConnectorDetail = (req, res) => {
  const { wx_id } = req.query
  
  const selectConnector = 'select * from easy_chat.users where wx_id = ?'
  db.query(selectConnector, wx_id, (err, results) => {
    if (err) return res.errHandle(err)
    
    res.send({
      code: 200,
      message: 'success',
      data: {
        detail: results[0]
      }
    })
  })
}

// 添加联系人 (发送)
exports.applicateConnector = (req, res) => {
  const { sender_id, receiver_id, remark, introduction } = req.query

  const addRelation = 'insert into easy_chat.user_relations set ?'
  db.query(addRelation, { wx_id: sender_id, friend_id: receiver_id, relation_status: 1, remark }, (err) => {
    if (err) return res.errHandle(err)

    db.query(addRelation, { wx_id: receiver_id, friend_id: sender_id, relation_status: 2, introduction }, (err, result) => {
      if (err) return res.errHandle(err)

      res.send({
        code: 200,
        message: 'success',
        data: {
          message: '发送成功'
        }
      })
    })
  })
}

// 添加联系人 (接受)
exports.acceptConnector = (req, res) => {
  const { sender_id, receiver_id, remark } = req.query

  const updateSenderRelation = 'update easy_chat.user_relations set relation_status = ?, remark = ? where wx_id = ? and friend_id = ?'
  db.query(updateSenderRelation, [0, remark, sender_id, receiver_id], (err) => {
    if (err) return res.errHandle(err)

    const updateReceiverRelation = 'update easy_chat.user_relations set relation_status = ? where wx_id = ? and friend_id = ?'
    db.query(updateReceiverRelation, [0, receiver_id, sender_id], (err, result) => {
      if (err) return res.errHandle(err)

      setTimeout(() => {
        res.send({
          code: 200,
          message: 'success',
          data: {
            message: '添加成功',
            user1_id: sender_id,
            user2_id: receiver_id
          }
        })
      }, 1000)
    })
  })
}
